/**
 * 
 */
package org.acsis.covoiturage.frontend.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.acsis.covoiturage.frontend.application.SVC;
import org.acsis.covoiturage.frontend.forms.LoginForm;
import org.acsis.covoiturage.services.IAuthentification;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 * @author Abdellah
 *
 */
public class LoginAction extends Action {

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		LoginForm lform = (LoginForm)form;
		
		HttpSession session = request.getSession();
		
		IAuthentification service = SVC.create(IAuthentification.class);
		
		String identifiantString= lform.getIdentifiant();
		String motdepasseString= lform.getMotDePasse();
		
		int loginOK = service.logIn(identifiantString,motdepasseString);
		
		if (loginOK==0) {
			session.setAttribute("user", service.getLoggedUtilisateur());
			return mapping.findForward("success");
			} else {
			ActionErrors errors = new ActionErrors();
			errors.add("loginEchec",new ActionMessage("erreur.loginEchec.required"));
			saveErrors(request, errors);
			return mapping.findForward("failed");
		}
	}

	
}
